using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.Domain.Repositories;
using Abp.Extensions;
using Abp.Linq.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.DomainEntities.RoomSourceVerifiy.DomainService;
using SOEI.Solution.DomainEntities.RoomSourceVerifiy.Dtos;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace SOEI.Solution.DomainEntities.RoomSourceVerifiy
{
    /// <summary>
    /// 【扩展模块】  <br/>
    /// 【功能描述】  ：XXX 模块<br/>
    /// 【创建日期】  ：2020.05.21 <br/>
    /// 【开发人员】  ：static残影<br/>
    ///</summary>
    [ApiExplorerSettings(GroupName = "Manager", IgnoreApi = true)]
    public class RoomVerifyPropertyAppService : SolutionAppServiceBase, IRoomVerifyPropertyAppService
    {
        /// <summary>
        ///【RoomVerifyProperty】仓储层
        /// </summary>
        private readonly IRepository<RoomVerifyProperty, long> _roomverifypropertyRepository;

        /// <summary>
        ///【RoomVerifyProperty】领域服务
        /// </summary>
        private readonly IRoomVerifyPropertyManager _roomverifypropertyManager;

        public RoomVerifyPropertyAppService(
            IRepository<RoomVerifyProperty, long> roomverifypropertyRepository,
            IRoomVerifyPropertyManager roomverifypropertyManager
        )
        {
            _roomverifypropertyRepository = roomverifypropertyRepository;
            _roomverifypropertyManager = roomverifypropertyManager;
        }

        #region -------------------------------------------------辅助工具生成---------------------------------------------- 

        /// <summary>
        ///【RoomVerifyProperty】获取的分页列表信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<PagedResultDto<RoomVerifyPropertyListDto>> GetPaged(GetRoomVerifyPropertysInput input)
        {
            var query = _roomverifypropertyRepository.GetAll()
                          //模糊搜索 字段PropertyOwnerName
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.PropertyOwnerName.Contains(input.FilterText))
                          //模糊搜索 字段PropertyOwnerIdentityCard
                          .WhereIf(!input.FilterText.IsNullOrWhiteSpace(), a => a.PropertyOwnerIdentityCard.Contains(input.FilterText))
            ;
            // TODO:根据传入的参数添加过滤条件

            var count = await query.CountAsync();

            var entityList = await query
                    .OrderBy(input.Sorting).AsNoTracking()
                    .PageBy(input)
                    .ToListAsync();

            var entityListDtos = ObjectMapper.Map<List<RoomVerifyPropertyListDto>>(entityList);

            return new PagedResultDto<RoomVerifyPropertyListDto>(count, entityListDtos);
        }

        /// <summary>
        ///【RoomVerifyProperty】通过指定id获取MemberListDto信息
        /// </summary>
        [AbpAuthorize()]
        public async Task<RoomVerifyPropertyListDto> GetById(EntityDto<long> input)
        {
            var entity = await _roomverifypropertyRepository.GetAsync(input.Id);

            var dto = ObjectMapper.Map<RoomVerifyPropertyListDto>(entity);
            return dto;
        }

        /// <summary>
        ///【RoomVerifyProperty】 获取编辑
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task<GetRoomVerifyPropertyForEditOutput> GetForEdit(NullableIdDto<long> input)
        {
            var output = new GetRoomVerifyPropertyForEditOutput();
            RoomVerifyPropertyEditDto editDto;

            if (input.Id.HasValue)
            {
                var entity = await _roomverifypropertyRepository.GetAsync(input.Id.Value);
                editDto = ObjectMapper.Map<RoomVerifyPropertyEditDto>(entity);
            }
            else
            {
                editDto = new RoomVerifyPropertyEditDto();
            }
            output.RoomVerifyProperty = editDto;
            return output;
        }
        /// <summary>
        ///【RoomVerifyProperty】 添加或者修改的公共方法
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task CreateOrUpdate(CreateOrUpdateRoomVerifyPropertyInput input)
        {

            if (input.RoomVerifyProperty.Id.HasValue)
            {
                await Update(input.RoomVerifyProperty);
            }
            else
            {
                await Create(input.RoomVerifyProperty);
            }
        }
        /// <summary>
        ///【RoomVerifyProperty】新增
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task<RoomVerifyPropertyEditDto> Create(RoomVerifyPropertyEditDto input)
        {
            //TODO:新增前的逻辑判断，是否允许新增

            var entity = ObjectMapper.Map<RoomVerifyProperty>(input);
            //调用领域服务
            entity = await _roomverifypropertyManager.CreateAsync(entity);

            var dto = ObjectMapper.Map<RoomVerifyPropertyEditDto>(entity);
            return dto;
        }

        /// <summary>
        ///【RoomVerifyProperty】编辑
        /// </summary>
        [AbpAuthorize()]
        protected virtual async Task Update(RoomVerifyPropertyEditDto input)
        {
            //TODO:更新前的逻辑判断，是否允许更新
            var key = input.Id.Value;
            var entity = await _roomverifypropertyRepository.GetAsync(key);
            //  input.MapTo(entity);
            //将input属性的值赋值到entity中
            ObjectMapper.Map(input, entity);
            await _roomverifypropertyManager.UpdateAsync(entity);
        }

        /// <summary>
        ///【RoomVerifyProperty】删除信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [AbpAuthorize()]
        public async Task Delete(EntityDto<long> input)
        {
            //TODO:删除前的逻辑判断，是否允许删除
            await _roomverifypropertyManager.DeleteAsync(input.Id);
        }

        /// <summary>
        ///【RoomVerifyProperty】 批量删除Member的方法
        /// </summary>
        [AbpAuthorize()]
        public async Task BatchDelete(List<long> input)
        {
            // TODO:批量删除前的逻辑判断，是否允许删除
            await _roomverifypropertyManager.BatchDelete(input);
        }

        #endregion

        #region -------------------------------------------------用户自定义------------------------------------------------
        /*请在此扩展应用服务实现*/
        #endregion
    }
}
